package com.tky.conference.ui;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.tky.conference.R;
import com.tky.conference.Tree.Node;
import com.tky.conference.Tree.SimpleTreeListViewAdapter;
import com.tky.conference.Tree.TreeHelper;
import com.tky.conference.Tree.TreeListViewAdapter;
import com.tky.conference.base.BaseActivity;
import com.tky.conference.dao.AppointInfo;
import com.tky.conference.dao.TreeInfo;
import com.tky.conference.entity.TreeBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by yy on 2015/12/18.
 */
public class TreeActivity extends BaseActivity {

    private List<TreeInfo> treeInfoList;
    private ListView treeView;
    private SimpleTreeListViewAdapter<TreeBean> mAdapter;
    private List<TreeBean> treeBeanList=new ArrayList<>();
    private Button bt;
    private Toolbar toolbar;

    @Override
    public void initView() {
        setContentView(R.layout.activity_tree);
        treeView= (ListView) findViewById(R.id.lv_tree);
        bt = (Button) findViewById(R.id.bt);
    }

    @Override
    public void initDates() {
        treeInfoList = (List<TreeInfo>) getIntent().getSerializableExtra("TREELIST");
        for (TreeInfo treeInfo:treeInfoList){
            TreeBean bean=new TreeBean(Integer.parseInt(treeInfo.getId()),Integer.parseInt(treeInfo.getPid()),treeInfo.getName());
            treeBeanList.add(bean);
        }

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = getIntent();
                List<Node> allNodes = mAdapter.getAllNodes();

                final StringBuilder builder=new StringBuilder();
                final StringBuilder nameBuilder=new StringBuilder();

                for (Node node : allNodes){
                    if (node.isLeaf() && node.isChecked()){
                        int id = node.getId();
                        String name = node.getName();
                        builder.append(id+",");
                        nameBuilder.append(name+"  ");
                    }
                }
                intent.putExtra("id",builder.toString());
                intent.putExtra("name",nameBuilder.toString() );
                TreeActivity.this.setResult(0, intent);
                TreeActivity.this.finish();
            }

        });
    }

    @Override
    public void initToolBar() {
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setTitle("人员列表");
        }
        setSupportActionBar(toolbar);
    }

    @Override
    public void initEvent() {
        try {
            mAdapter=new SimpleTreeListViewAdapter<TreeBean>(this,treeView,treeBeanList,0);
            treeView.setAdapter(mAdapter);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        mAdapter.setOnTreeNodeClickLitener(new TreeListViewAdapter.OnTreeNodeClickListener() {
            @Override
            public void onClick(Node node, int position) {
                if (node.isLeaf()){
                    if(node.isChecked()){
                        node.setChecked(false);
                    }else{
                        node.setChecked(true);
                    }
                    mAdapter.notifyDataSetChanged();
               }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if(id==android.R.id.home){
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
